## │  │  ├─ scene_gt_info.json
import json,os
import numpy as np
from tqdm import tqdm
import sys
import cv2

sys.path.append(os.getcwd())
from bbox.bbox_to_crop import crop_image
from bbox.create_bbox import mask_to_bbox, bbox_area


# 计算图像中的像素点个数

def main():
    folder = r'E:\pose\datasets\obj_ac\obj_000003\test\000001'
    length = len(os.listdir(f"{folder}/pose"))
    scene_gt_info = {}
    for i in tqdm(range(length)):
        image_path = f"{folder}/rgb/{str(i)}.jpg"
        mask_path = f"{folder}/mask/{str(i)}.jpg"
        mask = cv2.imread(mask_path)
        xyxy = mask_to_bbox(mask)
        cropped_image = crop_image(image_path, xyxy)
        px_count_all= np.count_nonzero(mask)

        # print(px_count_all)
        cv2.imwrite(f"{folder}/crop/{str(i)}.jpg", cropped_image)
        xyxy = [int(i) for i in xyxy]
        scene_gt_info[i] = [{
                                "bbox_obj": xyxy, 
                                 "bbox_visib": xyxy, 
                                 "px_count_all": px_count_all,
                                 'px_count_valid':px_count_all,
                                 "px_count_visib": px_count_all,
                                 "visib_fract": 1
                                 }]

    # Save scene_gt_info as a JSON file
    with open(f'{folder}/scene_gt_info.json', 'w') as f:
        json.dump(scene_gt_info, f)

if __name__ == "__main__":
    main()